package UserData;

import java.sql.SQLException;
import java.util.ArrayList;

import IOHelper.DataBaseHelper;

import po.TeacherPO;
/**
 * 
 * @author Flanker
 *
 */
public class TeacherData extends DataBaseHelper
{
	/**
	 * 
	 * @param aim
	 * @return
	 */
	public ArrayList<TeacherPO> getTeacher(TeacherPO aim)
	{
		ArrayList<TeacherPO> teacherList = new ArrayList<TeacherPO>();
		String sql = "SELECT ALL *FROM Teacher WHERE ";
		if (aim.teacherName.length() != 0)
		{
			sql += "teacherName ='" + aim.teacherName + "'";
		}
		if (aim.code.length() != 0)
		{
			sql += "code ='" + aim.code + "'";
		}
		if (aim.character.length() != 0)
		{
			sql += "character ='" + aim.character + "'";
		}
		if (aim.ID.length() != 0)
		{
			sql += "ID ='" + aim.ID + "'";
		}
		if (aim.department.length() != 0)
		{
			sql += "department ='" + aim.department + "'";
		}
		connect();

		try
		{
			stmt = con.prepareStatement(sql);
			resultset = stmt.executeQuery();
			con.commit();
			 int i=0;
			while (resultset.next())
			{
				TeacherPO tp = new TeacherPO();
				 System.out.println("--------"+i+"---------");
				tp.teacherName = resultset.getString(1);
				tp.code = resultset.getString(2);
				tp.character = resultset.getString(3);
				tp.ID = resultset.getString(4);
				tp.department = resultset.getString(5);
				System.out.println(tp.ID);
				teacherList.add(tp);
				 i++;
			}
		} catch (SQLException e)
		{
			e.printStackTrace();
		} finally
		{
			return teacherList;
		}
	}
	/**
	 * 
	 */
	public TeacherPO getTeacher(String id)
	{
		TeacherPO tp = new TeacherPO();
		String sql = "SELECT ALL * FROM Teacher WHERE ID = '" + id + "'";

		connect();
		try
		{
			stmt = con.prepareStatement(sql);
			resultset = stmt.executeQuery();
			con.commit();
			while (resultset.next())
			{
				tp.teacherName = resultset.getString(1);
				tp.code = resultset.getString(2);
				tp.character = resultset.getString(3);
				tp.ID = resultset.getString(4);
				tp.department = resultset.getString(5);
			}
		} catch (SQLException e)
		{
			e.printStackTrace();
			tp = null;
		} finally
		{
			reset();
			return tp;
		}
	}
	/**
	 * 
	 */
	public boolean updateTeacher(TeacherPO aim)
	{
		return delTeacher(aim.ID) && addTeacher(aim);
	}

	public boolean addTeacher(TeacherPO aim)
	{
		boolean isAdd = false;
		String sql = "INSERT INTO Teacher VALUES('" + aim.teacherName + "','"
				+ aim.code + "','" + aim.character + "','" + aim.ID + "','"
				+ aim.department + "')";
		connect();
		try
		{
			stmt = con.prepareStatement(sql);
			stmt.execute();
			isAdd = true;
		} catch (SQLException e)
		{
			e.printStackTrace();
			isAdd = false;
		} finally
		{
			reset();
			return isAdd;
		}
	}
	/**
	 * 
	 */
	public boolean delTeacher(String id)
	{
		boolean isDel = false;
		String sql = "DELETE * FROM Teacher WHERE ID ='" + id + "'";
		connect();

		try
		{
			stmt = con.prepareStatement(sql);
			stmt.execute();
			con.commit();
			isDel = true;
		} catch (SQLException e)
		{
			e.printStackTrace();
			isDel = false;
		} finally
		{
			reset();
			return isDel;
		}
	}
}
